package com.six.remark.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.six.core.entity.CheckShop;
import com.six.core.entity.Remark;
import com.six.core.util.JwtUtil;
import com.six.core.util.WebUtil;
import com.six.core.vo.Result;
import com.six.remark.dao.RemarkDao;
import com.six.remark.service.RemarkService;
import com.six.remark.vo.SearchRemark;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * ClassName:RemarkServiceImpl
 * Description:
 * date:2023/2/27 17:17
 *
 * @since JDK 1.8
 */
@Service
public class RemarkServiceImpl extends ServiceImpl<RemarkDao, Remark> implements RemarkService {
    @Autowired
    private RemarkDao remarkDao;
    @Override
    public Result findAll(SearchRemark searchRemark, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Remark> list = remarkDao.findAll(searchRemark);
        PageInfo<Remark> pageInfo = new PageInfo<>(list);
        return new Result(2000,"查询成功",pageInfo);
    }

    @Override
    public Result findAllRemark(String id,Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Remark> list = remarkDao.findAllRemark(id);
        for (Remark remark : list) {
            if (remark.getImg()!=null){
                String[] split = remark.getImg().split(",");
                remark.setImgs(split);
            }
        }
        PageInfo<Remark> pageInfo = new PageInfo<>(list);
        return new Result(2000,"查询成功",pageInfo);
    }

    @Override
    public Result findMyRemark(String goodsid,Integer currentPage, Integer pageSize) {
        String token = WebUtil.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.decodeJWT(token);
        Integer userid = (Integer) tokenChaim.get("tbUserid");
        PageHelper.startPage(currentPage,pageSize);
        List<Remark> list = remarkDao.findMyRemark(userid,goodsid);
        for (Remark remark : list) {
            if (remark.getImg()!=null){
                String[] split = remark.getImg().split(",");
                remark.setImgs(split);
            }
        }
        PageInfo<Remark> pageInfo = new PageInfo<>(list);
        return new Result(2000,"查询成功",pageInfo);
    }

    @Override
    public Result deleteById(Integer id) {
        remarkDao.deleteById(id);
        return new Result(2000,"删除成功");
    }

    @Override
    public Result insertRemark(Remark remark) {
        String token = WebUtil.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.decodeJWT(token);
        Integer tbuserid = (Integer) tokenChaim.get("tbUserid");
        remark.setUserid(tbuserid.toString());
        String[] imgs = remark.getImgs();
        StringBuffer buffer = new StringBuffer();

        for (String img : imgs) {
            buffer.append(img+",");
        }
        String substring = buffer.substring(0, buffer.length() - 1);
        remark.setImg(substring);
        remark.setGmtCreate(new Date());
        remark.setGmtModified(new Date());
        int i = remarkDao.insertRemark(remark);
        return new Result(2000,"添加成功",i);
    }

    @Override
    public Result update(Remark remark) {
        String[] imgs = remark.getImgs();
        StringBuffer buffer = new StringBuffer();
        for (String img : imgs) {
            buffer.append(img+",");
        }
        String substring = buffer.substring(0, buffer.length() - 1);
        remark.setImg(substring);
        int i = remarkDao.updateById(remark);
        return new Result(200,"修改成功",i);
    }
}
